//
//  BabyPlayerViewController.m
//  HappyBaby
//
//  Created by lanou on 15/11/26.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "BabyPlayerViewController.h"
#import "MoviePlayer.h"
#import "AppDelegate.h"
#import "Define.h"
#import "DetailView.h"
#import "DIYButton.h"
#import "BabyAnimationController.h"
@interface BabyPlayerViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)DetailModel *model;
@property(nonatomic,strong)MoviePlayer *moviePlayer;
@property(nonatomic,strong)DIYButton *collectionButton;
@property(nonatomic,strong)DIYButton *downloadButton;
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *page;

@property(nonatomic,strong)UILabel *descLabel;
@property(nonatomic,strong)UILabel *twoDescLabel;
@property(nonatomic,strong)UILabel *threeDescLabel;
@property(nonatomic,strong)UILabel *myCopyLabel;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation BabyPlayerViewController
{
    NSUInteger A;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    A = 0;
   
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(barbuttonAction:)];
    self.navigationItem.leftBarButtonItem = barbutton;
    
    
    DetailView *view = [[DetailView alloc]initWithFrame:CGRectMake(0, 340 * kScreenHeight / 667 - 20, kScreenWidth-200, 30)];
    //view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view];
    
    self.collectionButton = [DIYButton buttonWithType:UIButtonTypeCustom];
    //self.collectionButton.backgroundColor = [UIColor grayColor];
    self.collectionButton.frame = CGRectMake(kScreenWidth-10-30-40-5-30-40, 340 * kScreenHeight / 667 - 20,70, 30);
    self.collectionButton.iconImageView.image = [UIImage imageNamed:@"collect.png"];
    
    self.collectionButton.selectIconImageView.image = [UIImage imageNamed:@"truecollect.png"];
    self.collectionButton.textLabel.text = @"收藏";
    self.collectionButton.textLabel.textAlignment = NSTextAlignmentCenter;
    self.collectionButton.textLabel.textColor = [UIColor blackColor];
    self.collectionButton.textLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.collectionButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.collectionButton];
    
    self.downloadButton = [DIYButton buttonWithType:UIButtonTypeCustom];
    self.downloadButton.frame = CGRectMake(kScreenWidth-10-30-40, 340 * kScreenHeight / 667 - 20, 70, 30) ;
    self.downloadButton.iconImageView.image = [UIImage imageNamed:@"11.png"];
    self.downloadButton.selectIconImageView.image = [UIImage imageNamed:@"downloadCompleted.png"];
    self.downloadButton.textLabel.text = @"下载";
    self.downloadButton.textLabel.textAlignment = NSTextAlignmentCenter;
    self.downloadButton.textLabel.textColor = [UIColor blackColor];
    self.downloadButton.textLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.downloadButton addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.downloadButton];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, view.frame.origin.y + 30, kScreenWidth, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.5;
    [self.view addSubview:lineView];
    
    CGFloat x = 0;
    CGFloat y = kScreenHeight - self.downloadButton.frame.origin.y - 1;
    CGFloat width = kScreenWidth;
    CGFloat height = kScreenHeight - y;
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, kScreenWidth, height)];
    scrollView.contentSize = CGSizeMake(width * 2, height);
    scrollView.contentOffset = CGPointMake(width * 0, 0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    UIView *firView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    //firView.backgroundColor = [UIColor grayColor];
    UILabel *descLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, width, 30)];
    //descLabel.backgroundColor = [UIColor purpleColor];
    descLabel.font = [UIFont systemFontOfSize:14];
    [firView addSubview:descLabel];
    self.descLabel = descLabel;
    
    UILabel *twoDescLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, width, 30)];
    //twoDescLabel.backgroundColor = [UIColor yellowColor];
    twoDescLabel.font = [UIFont systemFontOfSize:14];
    [firView addSubview:twoDescLabel];
    self.twoDescLabel = twoDescLabel;
    UILabel *threeDescLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, width, 30)];
    //threeDescLabel.backgroundColor = [UIColor redColor];
    threeDescLabel.font = [UIFont systemFontOfSize:14];
    [firView addSubview:threeDescLabel];
    
    [scrollView addSubview:firView];
    self.threeDescLabel = threeDescLabel;  
    
    
    UIView *secView = [[UIView alloc]initWithFrame:CGRectMake(width * 1, 0, width, height)];
    //secView.backgroundColor = [UIColor greenColor];
    UILabel *myCopyLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, width-20, 100)];
    myCopyLabel.font = [UIFont systemFontOfSize:15];
    //myCopyLabel.backgroundColor = [UIColor magentaColor];
    [secView addSubview:myCopyLabel];
    self.myCopyLabel = myCopyLabel;
    [scrollView addSubview:secView];
    
    //self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(dynamic) userInfo:nil repeats:YES];
    
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((width-30)/2, kScreenHeight - 160, 30, 30)];
    //pageControl.backgroundColor = [UIColor purpleColor];
    pageControl.numberOfPages = 2;
    [self.view addSubview:pageControl];
    pageControl.currentPage = 0;
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    pageControl.pageIndicatorTintColor = [UIColor yellowColor];
    [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    self.page = pageControl;
    if (self.url)
    {
        [self sessonDataWithUrl:self.url];
    }
    else if (self.moreUrl)
    {
        [self sessonDataWithUrl:self.moreUrl];
    }
    else if (self.topUrl)
    {
        
        [self sessonDataWithUrl:self.topUrl];
        
    }
    [self.moviePlayer.back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
   
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dynamic) userInfo:nil repeats:YES];
    
    
}

//根据字符串和字体求字符串的高度
- (CGSize)stringSizeWithFont:(UIFont *)font string:(NSString *)string width:(CGFloat)width
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    return rect.size;
    
}


- (void)pageAction:(UIPageControl *)page
{
    UIScrollView *scrollView = self.view.subviews[4];
    [scrollView setContentOffset:CGPointMake(page.currentPage * kScreenWidth, 0) animated:NO];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
        CGFloat x = scrollView.contentOffset.x;
       self.page.currentPage = x/kScreenWidth;

}


//解析数据；
- (void)sessonDataWithUrl:(NSString *)url
{
    
    [[[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil ];
        NSDictionary *dataDic = [dic valueForKey:@"data"];
        
            self.model = [[DetailModel alloc]init];
            [_model setValuesForKeysWithDictionary:dataDic];
            NSArray *array = [dataDic objectForKey:@"editors"];
            for (NSDictionary *dic in array)
            {
                _model.nickname = [dic objectForKey:@"nickname"];
               
            }

     dispatch_async(dispatch_get_main_queue(), ^{
         MoviePlayer *moviePlayer = [[MoviePlayer alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 340 * kScreenHeight / 667 - 20) URL:[NSURL URLWithString:self.model.video]];
         moviePlayer.topLabel.text = self.model.title;
         moviePlayer.backgroundColor = [UIColor whiteColor];
         
         [self.view addSubview:moviePlayer];
         self.view.backgroundColor = [UIColor whiteColor];
         self.moviePlayer = moviePlayer;
         
         //动态文字；
         [self dynamic];
         
         if (self.model.myDescription)
         {
             self.descLabel.text = [NSString stringWithFormat:@"简介:%@",self.model.myDescription];
             
         }
         
         //如果model存在,进行赋值，如果不存在，让下一个frame等于第二个frame;
         if (self.model.nickname != nil)
         {
             self.twoDescLabel.text = [NSString stringWithFormat:@"听译:%@",self.model.nickname];
         }
         else
         {
             self.threeDescLabel.frame = self.twoDescLabel.frame;
         }
         
         
         if (self.model.editor != nil)
         {
             self.threeDescLabel.text = [NSString stringWithFormat:@"上传:%@",self.model.editor];
         }
     });
        
    }]resume];
   
}

//动态文字
- (void)dynamic
{
    if (self.model.myCopy.length > A)
    {
        self.myCopyLabel.text = [self.model.myCopy substringToIndex:A++];
    }
    
    self.myCopyLabel.height = [self stringSizeWithFont:self.myCopyLabel.font string:self.model.myCopy width:self.myCopyLabel.frame.size.width].height;
    self.myCopyLabel.numberOfLines = 0;

    
}

//全屏返回按钮的执行方法；
- (void)back:(UIButton *)btn
{
    //AppDelegate *appdelegate = [UIApplication sharedApplication].delegate;
    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//collect按钮执行方法；
- (void)collect:(DIYButton *)btn
{
    
    
}
//下载按钮执行的方法；
- (void)download:(DIYButton *)btn
{
    
    
}

//停止视频播放器；
- (void)barbuttonAction:(UIBarButtonItem *)btn
{
    [self.moviePlayer.moviePlayer stop];
    self.moviePlayer.moviePlayer = nil;
    [self.moviePlayer.timer invalidate];
    [self.timer invalidate];
    

    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMovieDurationAvailableNotification object:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMovieDurationAvailableNotification object:nil];  
}
- (void)dealloc
{

}

@end
